package com.cloudfun.campusshare.dao;

import com.cloudfun.campusshare.common.model.vo.AppListVO;
import com.cloudfun.campusshare.common.response.PageResult;
import com.cloudfun.campusshare.common.security.AuthenticationHolder;
import com.cloudfun.campusshare.entity.AppEntity;
import com.cloudfun.campusshare.repo.AppEntityRepo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author linyangping
 * @date 2019/11/6
 */
@Repository
public class AppDao {

    @PersistenceContext
    private EntityManager entityManager;

    @Autowired
    private AppEntityRepo appEntityRepo;

    public PageResult<AppListVO> findByCondition(Pageable pageable, String value) {
        Specification<AppEntity> specification = (root, query, criteriaBuilder) -> {
            List<Predicate> predicateList = new ArrayList<>();
            // 非管理员用户
            if (!AuthenticationHolder.isAdmin()) {
                predicateList.add(criteriaBuilder.equal(root.get("userId"), AuthenticationHolder.getUserId()));
            }
            if (StringUtils.isNotBlank(value)) {
                List<Predicate> like = new ArrayList<>();
                Predicate idLikePredicate = criteriaBuilder.like(root.get("id"), "%" + value + "%");
                like.add(idLikePredicate);
                Predicate appKeyLikePredicate = criteriaBuilder.like(root.get("appKey"), "%" + value + "%");
                like.add(appKeyLikePredicate);
                Predicate nameLikePredicate = criteriaBuilder.like(root.get("name"), "%" + value + "%");
                like.add(nameLikePredicate);
                Predicate[] predicates = new Predicate[like.size()];
                predicateList.add(criteriaBuilder.or(like.toArray(predicates)));
            }

            Predicate[] predicates = new Predicate[predicateList.size()];
            predicateList.toArray(predicates);
            return criteriaBuilder.and(predicates);
        };
        Page<AppEntity> entityPage = appEntityRepo.findAll(specification, pageable);
        long totalElements = entityPage.getTotalElements();
        List<AppListVO> content = entityPage.getContent().stream().map(appEntity -> {
            AppListVO vo = new AppListVO();
            vo.setAppId(appEntity.getId());
            vo.setAppName(appEntity.getName());
            vo.setVersion(appEntity.getAppVersion());
            vo.setStatus(appEntity.getStatus());
            vo.setCreateTime(appEntity.getCreateTime());
            return vo;
        }).collect(Collectors.toList());
        PageResult<AppListVO> result = new PageResult<>();
        result.setElements(content);
        result.setTotalSize(totalElements);
        return result;
    }

}
